#include "epoller.h"
Epoller::Epoller(int maxEvent)
    :Epollfd_(epoll_create(1024)),
    events_(maxEvent)
{
    assert(Epollfd_ >= 0 && maxEvent >0);
}
Epoller::~Epoller()
{
    close(Epollfd_);
}
bool Epoller::AddFd_(int fd_,uint32_t events)
{
    assert(fd_ >0);
    struct epoll_event evt;
    bzero(&evt,sizeof(evt));
    evt.data.fd = fd_;
    evt.events |=events;
    return 0 == epoll_ctl(Epollfd_,EPOLL_CTL_ADD,fd_,&evt);
}
bool Epoller::ModFd_(int fd_,uint32_t events)
{
    assert(fd_ >0);
    struct epoll_event evt;
    bzero(&evt,sizeof(evt));
    evt.data.fd = fd_;
    evt.events |=events;
    return 0 == epoll_ctl(Epollfd_,EPOLL_CTL_MOD,fd_,&evt);
}
bool Epoller::DelFd_(int fd_)
{
    assert(fd_ >0);
    return 0 == epoll_ctl(Epollfd_,EPOLL_CTL_DEL,fd_,0);
}
//返回 已经就绪的事件数量
int Epoller::Wait_(int timeout)
{
    return epoll_wait(Epollfd_,&events_[0],1024,timeout);
}
//返回某个事件的 描述符
int Epoller::GetEventFd_(size_t i) const
{
    assert(i< events_.size());
    return events_.at(i).data.fd;
}
//返回某个事件的 触发结构
uint32_t Epoller::GetEvent_(size_t i) const
{
    assert( i<events_.size() );
    return events_.at(i).events;
}